package com.jike.shop.cloud.service;

import com.alibaba.fastjson.JSON;
import com.jike.shop.cloud.IShopService;
import com.jike.shop.cloud.IUserService;
import com.jike.shop.cloud.entity.ShopCartEntity;
import com.jike.shop.cloud.mapper.ShopCartMapper;
import com.jike.shop.cloud.request.AddShopReq;
import com.jike.shop.cloud.response.CommonResp;
import com.jike.shop.cloud.user.dto.Authentication;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@DubboService(group = "jike-shop", version = "1.0.0", protocol = "dubbo")
@Slf4j
public class ShopServiceImpl implements IShopService {

    @DubboReference(group = "jike-shop", version = "1.0.0")
    private IUserService userService;

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Override
    public String test(Long id) {
        return null;
    }

    @Override
    public CommonResp<?> addShopCart(AddShopReq addShopReq) {
        // 1.校验用户信息
        validUser();
        // 2.查看购物车是否添加过商品
        ShopCartEntity shopCartEntity = shopCartMapper.selectAllByUserIdAndProductId("jikeshop", addShopReq.getProductId());
        if (shopCartEntity != null) {
            // 2.1、未添加、新增购物车信息
            ShopCartEntity shopCartEntity1 = JSON.parseObject(JSON.toJSONString(addShopReq), ShopCartEntity.class);
            shopCartEntity.setProductId(addShopReq.getProductId());
            shopCartEntity.setProductNum(addShopReq.getProductNum());
            shopCartEntity.setProductSkuId(addShopReq.getProductSkuId());
            shopCartEntity.setUserId("jikeshop");
            shopCartEntity.setStatus("10A");
            shopCartEntity.setCreateDate(new Date());
            shopCartEntity.setUpdateDate(new Date());
            shopCartMapper.insert(shopCartEntity);
        } else {
            // 2.2、已添加、修改购物车信息
            shopCartEntity.setProductNum(shopCartEntity.getProductNum() + addShopReq.getProductNum());
        }
        return null;
    }

    @Override
    public CommonResp<?> placeOrder() {

        return null;
    }

    /**
     * 校验用户
     */
    private void validUser() {
        Authentication authentication = null;
        try {
            authentication = userService.login("jikeshop", "12345678");
            log.info("authentication: {}", authentication);
        } catch (Exception e) {

        }
        // 判断token过期时间是否小于支付时间(30分钟)
        assert authentication != null;
        if (authentication.getExpiresIn() < 30) {
            Authentication refresh = userService.refresh(authentication.getRefreshToken());
            log.info("refresh: {}", refresh);
        }
    }

    /**
     * 校验商品
     *
     * @param productId  id
     * @param productNum 数量
     */
    private void validProduct(Long productId, Integer productNum) {
        // 1、判断商品是否存在、商品是否是上架状态；如果不行，就抛出“商品不可售”的异常；

        // 2、判断商品库存，如果库存不足，也不能把商品添加到购物车，抛出“商品库存不足异常；
    }
}
